package com.ica.android.ui.policy;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import com.ica.R;
import com.ica.android.ui.policy.time.TimeTableManagerActivity;
import com.ica.model.command.AbstractCommand;
import com.ica.model.contact.ContactGroup;
import com.ica.model.facade.PolicyFacadeMockImpl;
import com.ica.model.policy.time.TimeStamp;
import com.ica.model.policy.time.utils.TimeUtils.DAYS;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class EditPolicyActivity extends PolicyActivity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init(R.layout.edit_policy);        
        Bundle extras = getIntent().getExtras();
        long policyId = (Long) extras.get("policyId");
        policy = policyFacade.getElement(policyId);
        etName.setText(policy.getName());
        List<ContactGroup> cg = policy.getContacts();
        if (cg != null)
        	for(ContactGroup contactGroup : cg)
        		checkedContacts[getIndexContact(contactGroup.getId())]=true;
        AbstractCommand c = policy.getCommand();
        if (c != null)
        	lastCommand = getIndexCommand(c.getId());
        timeFilter = new HashMap<DAYS, List<TimeStamp>>();
        for(DAYS day : DAYS.values()) {
    		timeFilter.put(day,new Vector<TimeStamp>(policy.getTimeStamps(day)));
        }
        TimeTableManagerActivity.setTimeTable(timeFilter);
    }
	
	private int getIndexContact(long id) {	
		for(int i=0;i<contacts.length;i++)
			if (contacts[i].getId()==id) return i;
		return -1;
	}
	
	private int getIndexCommand(long id) {	
		for(int i=0;i<commands.length;i++)
			if (commands[i].getId()==id) return i;
		return -1;
	}

	@Override
	protected OnClickListener onClickSave() {
		return new OnClickListener() {		
			public void onClick(View v) {
				if (etName.getText().toString().equals(""))
					Toast.makeText(EditPolicyActivity.this, "The Policy's name is required", Toast.LENGTH_SHORT).show();
				else {
					policy.setName(etName.getText().toString());
					List<ContactGroup> c = new Vector<ContactGroup>();
					for(int i=0;i<contacts.length;i++) {
						if (checkedContacts[i]) c.add(contacts[i]);
					}
					policy.setContacts(c);
					if (command != null)
						policy.setCommand(command);
					policy.replaceAllTimeStamps(timeFilter);
					PolicyFacadeMockImpl.getInstance().replaceElement(policy.getId(), policy);
					TimeTableManagerActivity.clearResult();
					setResult(RESULT_OK,null);
					finish();
				}
			}
		};	
	}
	
	@Override
	protected OnClickListener onClickTimeTable() {
		return new OnClickListener() {		
			public void onClick(View v) {
				Intent intent = new Intent(EditPolicyActivity.this, TimeTableManagerActivity.class);
				startActivity(intent);
			}
		};
	}
}
